package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.common.Result;
import com.atguigu.spzx.model.entity.sys.SysRole;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "角色管理模块")
//@CrossOrigin(originPatterns = "*",allowCredentials = "true")
@RequestMapping("/admin/system/role")
public class SysRoleController {
    @Resource
    SysRoleService sysRoleService;
    @Operation(summary = "查询所有角色以及角色是否已分配")
    @GetMapping("findRolesAndIsChecked/{userId}")
    public Result findRolesAndIsChecked(@PathVariable("userId")Long userId){
        //查询所有的角色， 然后根据userId查询该用户已分配的角色，最后设置所有角色列表中已分配的角色默认选中
        List<SysRole> sysRoleList = sysRoleService.findRolesAndIsChecked(userId);
        return Result.ok().data(sysRoleList);
    }

    @Operation(summary = "条件查询角色分页列表")
    @GetMapping("page/{pageNum}/{pageSize}")// /admin/system/role/page/1/3?keyword=xxx
    public Result page(@PathVariable("pageNum")Integer pageNum,
                       @PathVariable("pageSize")Integer pageSize,
                       String keyword){
        PageInfo<SysRole> pageInfo = sysRoleService.page(pageNum,pageSize , keyword);
        return Result.ok().data(pageInfo);
    }

    @Operation(summary = "删除角色接口")
    @DeleteMapping("deleteById/{id}")// /admin/system/role/page/1/3?keyword=xxx
    public Result deleteById(@PathVariable("id")Long id){
        sysRoleService.deleteById(id);
        return Result.ok();
    }

    //修改：先根据id查询回显、然后更新
    @Operation(summary = "根据id查询角色接口")
    @GetMapping("getById/{id}")// /admin/system/role/page/1/3?keyword=xxx
    public Result getById(@PathVariable("id")Long id){
        SysRole role = sysRoleService.getById(id);
        return Result.ok().data(role);
    }
    @Operation(summary = "根据id更新角色接口")
    @PutMapping("updateById")// /admin/system/role/page/1/3?keyword=xxx
    public Result updateById(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }
    //新增
    @Operation(summary = "新增角色接口")
    @PostMapping("save")// /admin/system/role/page/1/3?keyword=xxx
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.ok();
    }
}
